//
//  guanzhuTableViewController.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/7.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "guanzhuTableViewController.h"
#import "dengluViewController.h"
#import "ENPdetail.h"
#import "News.h"
#import "CustomCell.h"
#import <BmobSDK/Bmob.h>

@interface guanzhuTableViewController ()<UISearchBarDelegate>{

    NSMutableArray *_gzinfoMutableArray;
}

@property (weak, nonatomic) IBOutlet UISearchBar *gzsearchbar; //为搜索栏添加输出口
//@property (nonatomic, strong) NSMutableArray *gzlistFilterTeams; //搜索栏过滤

@end

@implementation guanzhuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //通知模式  为了传递 登录界面的用户名 因为有了更好的方法 所以注释掉这段
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
    
    
    //设置搜索栏委托对象为当前视图控制器
    self.gzsearchbar.delegate = self;
    
    _gzinfoMutableArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    //初次进入查询所有数据
    [self gzfilterContentForSearchText:@""];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//通知中心 调用方法
- (void)ChangeNameNotification:(NSNotification *)notification {
    
    NSDictionary *nameDictionary = [notification userInfo];
    self.navigationItem.title = [nameDictionary objectForKey:@"name"];
}


//点击searchBar旁边的Cancel按钮  退出键盘
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [_gzsearchbar resignFirstResponder];
}

#pragma mark Content Filtering
- (void)gzfilterContentForSearchText:(NSString*)searchText;
{
    //获取当前用户对象
    BmobUser *tempuser = [BmobUser getCurrentUser];
    if (tempuser) {
        _tempname = [tempuser objectForKey:@"username"];
        self.navigationItem.title=_tempname;
    }
    else{
        
        self.navigationItem.title=@"请登录后使用关注";
        _tempname =@"kongbiao";
    }
    
    if(!searchText||searchText.length<=0)
    {
        [_gzinfoMutableArray removeAllObjects];
        
        BmobQuery *query = [BmobQuery queryWithClassName:_tempname];
        query.limit = 20;
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                News *info    = [[News alloc] init];
                if ([obj objectForKey:@"Ename"]) {
                    info.Ename = [obj objectForKey:@"Ename"];
                }
                if ([obj objectForKey:@"city"]) {
                    info.city = [obj objectForKey:@"city"];
                }
                if ([obj objectForKey:@"call"]) {
                    info.call = [obj objectForKey:@"call"];
                }
                if ([obj objectForKey:@"adress"]) {
                    info.adress = [obj objectForKey:@"adress"];
                }
                if ([obj objectForKey:@"email"]) {
                    info.email = [obj objectForKey:@"email"];
                }
                if ([obj objectForKey:@"lawman"]) {
                    info.lawman = [obj objectForKey:@"lawman"];
                }
                if ([obj objectForKey:@"setmoney"]) {
                    info.setmoney = [obj objectForKey:@"setmoney"];
                }
                if ([obj objectForKey:@"setdate"]) {
                    info.setdate = [obj objectForKey:@"setdate"];
                }

                [_gzinfoMutableArray addObject:info];
            }
            
            [self.tableView reloadData];
        }];

}
    else{
        [_gzinfoMutableArray removeAllObjects];
        
        BmobQuery *gzquery = [BmobQuery queryWithClassName:_tempname];
        [gzquery whereKey:@"Ename" matchesWithRegex:searchText ];
        gzquery.limit = 20;
        [gzquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                News *info    = [[News alloc] init];
                if ([obj objectForKey:@"Ename"]) {
                    info.Ename = [obj objectForKey:@"Ename"];
                }
                if ([obj objectForKey:@"city"]) {
                    info.city = [obj objectForKey:@"city"];
                }
                if ([obj objectForKey:@"call"]) {
                    info.call = [obj objectForKey:@"call"];
                }
                if ([obj objectForKey:@"adress"]) {
                    info.adress = [obj objectForKey:@"adress"];
                }
                if ([obj objectForKey:@"email"]) {
                    info.email = [obj objectForKey:@"email"];
                }
                if ([obj objectForKey:@"lawman"]) {
                    info.lawman = [obj objectForKey:@"lawman"];
                }
                if ([obj objectForKey:@"setmoney"]) {
                    info.setmoney = [obj objectForKey:@"setmoney"];
                }
                if ([obj objectForKey:@"setdate"]) {
                    info.setdate = [obj objectForKey:@"setdate"];
                }

                [_gzinfoMutableArray addObject:info];
            }
            
            [self.tableView reloadData];
        }];
       // self.gzlistFilterTeams = [NSMutableArray arrayWithArray:self->_gzinfoMutableArray];
        
    }
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_gzinfoMutableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"guanzhuresult";
        
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        News *info = (News*)[_gzinfoMutableArray objectAtIndex:indexPath.row];
        cell.gzEnamelabel.text= info.Ename;
        
        return cell;
    }
#pragma mark --UISearchBarDelegate 协议方法
//  获得焦点，成为第一响应者
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.gzsearchbar sizeToFit];
    [self gzfilterContentForSearchText:@""];
    return YES;
}
//点击键盘上的搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.gzsearchbar.showsScopeBar = NO;
    [self.gzsearchbar sizeToFit];
    [self.gzsearchbar resignFirstResponder];
}
//当文本内容发生改变时候调用
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self gzfilterContentForSearchText:self.gzsearchbar.text ];
    [self.tableView reloadData];
}

//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    BmobUser *tempusr2 =[BmobUser getCurrentUser];

    if(tempusr2){
      if([segue.identifier isEqualToString:@"gzshowdetail"])
       {
        ENPdetail *detailViewController = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        
        News *info = (News*)[_gzinfoMutableArray objectAtIndex:selectedIndex];
        detailViewController.title = info.Ename;
        detailViewController.detailenpname = info.Ename;
        detailViewController.detailcallname = info.call;
        detailViewController.detailadressname = info.adress;
        detailViewController.detailmailname = info.email;
        detailViewController.lawmanname = info.lawman;
        detailViewController.setdatename = info.setdate;
        detailViewController.setmoneyname = info.setmoney;
       }
    }
}

//滑动删除委托协议
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //实现滑动删除关注信息动作
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        News *info = (News*)[_gzinfoMutableArray objectAtIndex:selectedIndex];
       
        // NSLog(@"%@",info.Ename);
        
         BmobQuery   *bquery = [BmobQuery queryWithClassName:_tempname];
         [bquery whereKey:@"Ename" matchesWithRegex:info.Ename];
         [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error){
         for (BmobObject *obj in array) {
             if ([obj objectForKey:@"Ename"]) {
         [obj deleteInBackground];
            
             }
         }
        }];
         [self.tableView reloadData];
         
        //实现滑动删除动画
        [_gzinfoMutableArray removeObjectAtIndex:indexPath.row];
        // 删除当前表中数据
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    /*   滑动插入条件判定 暂时用不到此方法
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
     */
}
@end
